package com.puman.beautyfarmcounselor.ui.task.adpter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.puman.beautyfarmcounselor.MainActivity;
import com.puman.beautyfarmcounselor.R;
import com.puman.beautyfarmcounselor.ui.customer.CustomerActivity_;
import com.puman.beautyfarmcounselor.ui.task.model.GroupTask;
import com.puman.beautyfarmcounselor.ui.task.view.SlideView;
import com.puman.beautyfarmcounselor.ui.task.view.SlideView.OnSlideListener;

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public class MemberListAdapter extends BaseExpandableListAdapter {

	public static final int ItemHeight = 48;
	public static final int PaddingLeft = 38;
	private LayoutInflater mInflater;
	private List<GroupTask> groupTaskList = new ArrayList<>();
	private Context parentContext;
	private OnSlideListener slideList;
	private Map<Integer, SlideView> recordMap = new HashMap<Integer, SlideView>();



	public final class ViewHolder{
		public TextView name;
		public ViewGroup deleteHolder;
		public RelativeLayout itemLayout;
		ViewHolder(View view){
			name = (TextView) view.findViewById(R.id.member_list_name);
			itemLayout = (RelativeLayout) view.findViewById(R.id.member_item_layout);
		}
	}

	public final class ParentViewHolder {
		public TextView name;

	}

	public MemberListAdapter(Context context, List<GroupTask> groupTaskList, OnSlideListener slideList) {
		parentContext = context;
		this.groupTaskList = groupTaskList;
		this.mInflater = LayoutInflater.from(context);
		this.slideList = slideList;
	}

	public List<GroupTask> getTreeNode() {
		return groupTaskList;
	}

	public void updateTreeNode(List<GroupTask> nodes) {
		groupTaskList = nodes;
	}

	public void removeAll() {
		groupTaskList.clear();
	}
	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return groupTaskList.get(groupPosition).getChildTaskList()
				.get(childPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public View getChildView(final int groupPosition, final int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		final ViewHolder holder;
		SlideView slideView;
		if (recordMap.get(groupPosition * 100 + childPosition) == null) {
			slideView = (SlideView) convertView;
			View itemView = mInflater.inflate(R.layout.list_item, null);

			slideView = new SlideView(parentContext);
			slideView.setContentView(itemView);

			holder = new ViewHolder(slideView);

			slideView.setOnSlideListener(slideList);
			recordMap.put(groupPosition * 100 + childPosition, slideView);
			slideView.setTag(holder);
		} else {
			slideView = recordMap.get(groupPosition * 100 + childPosition);
			holder = (ViewHolder) slideView.getTag();
		}
		groupTaskList.get(groupPosition).getChildTaskList().get(childPosition).slideView = slideView;
		holder.name.setText(groupTaskList.get(groupPosition).getChildTaskList().get(childPosition).getName());
		holder.itemLayout.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent=new Intent();
                intent.setClass(MainActivity.getInstace(), CustomerActivity_.class);
                MainActivity.getInstace().startActivity(intent);
			}
		});
		return slideView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		// TODO Auto-generated method stub
		return groupTaskList.get(groupPosition).getChildTaskList().size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		// TODO Auto-generated method stub
		return groupTaskList.get(groupPosition).getName();
	}

	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		return groupTaskList.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		final ParentViewHolder parentViewHolder;
		if (convertView == null) {
			parentViewHolder = new ParentViewHolder();

			convertView = mInflater.inflate(R.layout.lost_parent_item, null);
			parentViewHolder.name = (TextView) convertView
					.findViewById(R.id.name);
			convertView.setTag(parentViewHolder);
		} else {
			parentViewHolder = (ParentViewHolder) convertView.getTag();

		}
		parentViewHolder.name
				.setText(groupTaskList.get(groupPosition).getName());

		return convertView;
	}


	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return true;
	}


}
